module.exports = {
  //扫描二维码
  scanErMa:function(app,options){
    var that=this;
    var res = wx.getStorageSync("loginUserInfo");
    var currentOpenId="";
    var currentId="";
    if (res) {
      app.globalData.loginUserInfo = res;
      console.log(app.globalData.baseUrl + "/member/login?" + app.globalData.loginUserInfo);
      console.log("有值二维码页面loginUserInfo=" + JSON.stringify(res));
      console.log("有值请求参数=options" + JSON.stringify(options));
      var fenxiangStr =  decodeURIComponent(options.scene);
      console.log("有值请求参数options.scene=" + options.scene);
      console.log("有值请求参fenxiangStr=" + fenxiangStr);
      if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
      }else {
        console.log("有值进入发展下级接口开始fenxiangStr="+fenxiangStr);
        var parentid = fenxiangStr.split("&");
        console.log("有值进入发展下级接口开始parentid="+JSON.stringify(parentid));
        var parentMemberId="";
        for(var i=0;i<parentid.length;i++){
          var s=parentid[i].split("=");
          console.log("有值s="+JSON.stringify(s));
          if(s[0]=="parentId"){
            parentMemberId =s[1];
          }
        }
        var requestJson = {
          parentId: parentMemberId,
          id: app.globalData.loginUserInfo.userInfo.id,
          openId: app.globalData.loginUserInfo.userInfo.openId,
          enterType: 0
        }
        console.log("没值发展下级requestJson="+JSON.stringify(requestJson));
        if (app.globalData.loginUserInfo.status != 100) {
          that.memberLogin(requestJson,app);
          //调用初始化方法
        }
      }
    } else {
      //app.cwxLoginGaiBan();
      //调用登录获取openId
      wx.login({
        success: function(res) {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res);
          var json = JSON.stringify({ code: res.code });
          wx.request({
            url: app.globalData.baseUrl + "/member/getWeChatMemberResult",
            data: json,
            header: app.globalData.headerPost,
            method: 'POST',
            success: function (e) {
              console.log("/member/getWeChatMemberResult 登录成功"+JSON.stringify(e.data));
              currentOpenId= e.data.data.openId;
              currentId = e.data.data.memberId;
            },
            fail: function (e) {
              console.log("/member/getWeChatMemberResult 调用失败="+e);
              wx.showLoading({
                title: '微信登录失败',
                mask: true
              });
            },
            complete:function(){
              wx.hideLoading();
              console.log("/member/getWeChatMemberResult 调用完成complete");
              console.log("没值值二维码页面loginUserInfo=" + JSON.stringify(res));
              console.log("没值请求参数options=" + JSON.stringify(options));
              var fenxiangStr =decodeURIComponent(options.scene);
              console.log("没值请求参数options.scene=" + options.scene);
              console.log("没值请求参fenxiangStr=" + JSON.stringify(fenxiangStr));
              if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
                console.log("没值不是分享进入的fenxiangStr=" + fenxiangStr);

              }else {
                console.log("没值进入发展下级接口开始fenxiangStr="+fenxiangStr);
                var parentid = fenxiangStr.split("&");
                console.log("没值进入发展下级接口开始parentid="+JSON.stringify(parentid));
                var parentMemberId="";
                for(var i=0;i<parentid.length;i++){
                  var s=parentid[i].split("=");
                  console.log("没值s="+JSON.stringify(s));
                  if(s[0]=="parentId"){
                    parentMemberId =s[1];
                  }
                }
                var requestJson = {
                  parentId: parentMemberId,
                  id: currentId,
                  openId: currentOpenId,
                  enterType: 0
                }
                console.log("没值发展下级requestJson="+JSON.stringify(requestJson));
                if (app.globalData.loginUserInfo.status != 100) {
                  that.memberLogin(requestJson,app);
                  //调用初始化方法
                }

              }
            }
          })
        },
        fail:function(res){
          console.log("wx.login失败="+JSON.stringify(res.data));
        },
        complete:function(res){
          wx.hideLoading();
          console.log("调用登录wx.login 完成" + JSON.stringify(res.data));
        }
      })
    }
  },

  //分享转发逻辑
  shareFaZhanXiaJi:function(app,options){
    var that=this;
    var res = wx.getStorageSync("loginUserInfo");
    var currentOpenId="";
    var currentId="";
    if (res) {
      app.globalData.loginUserInfo = res;
      console.log(app.globalData.baseUrl + "/member/login?" + app.globalData.loginUserInfo);
      console.log("有值二维码页面loginUserInfo=" + JSON.stringify(res));
      console.log("有值请求参数=options" + JSON.stringify(options));
      var fenxiangStr = options;
      console.log("有值请求参数options.parentId=" + options.parentId);
      console.log("没值请求参数options.enterType=" + options.enterType);
      console.log("没值请求参fenxiangStr=" + JSON.stringify(fenxiangStr));
      if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
        console.log("有值不是分享进入的fenxiangStr=" + fenxiangStr);

      }else {
        console.log("有值是分享进入的fenxiangStr=" + fenxiangStr);
        if (fenxiangStr.enterType == 0) {//分享发展下级类型
          var requestJson = {
            parentId: fenxiangStr.parentId,
            id: app.globalData.loginUserInfo.userInfo.id,
            openId: app.globalData.loginUserInfo.userInfo.openId,
            enterType: 0
          }
          console.log("有值发展下级requestJson=" + JSON.stringify(requestJson));
          if (app.globalData.loginUserInfo.status != 100) {
            that.memberLogin(requestJson,app);
            //调用初始化方法
          }
        }
      }
    } else {
      //app.cwxLoginGaiBan();
      //调用登录获取openId
      wx.login({
        success: function(res) {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res);
          var json = JSON.stringify({ code: res.code });
          wx.request({
            url: app.globalData.baseUrl + "/member/getWeChatMemberResult",
            data: json,
            header: app.globalData.headerPost,
            method: 'POST',
            success: function (e) {
              console.log("/member/getWeChatMemberResult 登录成功"+JSON.stringify(e.data));
              currentOpenId= e.data.data.openId;
              currentId = e.data.data.memberId;
            },
            fail: function (e) {
              console.log("/member/getWeChatMemberResult 调用失败="+e);
              wx.showLoading({
                title: '微信登录失败',
                mask: true
              });
            },
            complete:function(){
              wx.hideLoading();
              console.log("/member/getWeChatMemberResult 调用完成complete");
              console.log("没值值二维码页面loginUserInfo=" + JSON.stringify(res));
              console.log("没值请求参数=options" + JSON.stringify(options));
              var fenxiangStr = options;
              console.log("没值请求参数options.parentId=" + options.parentId);
              console.log("没值请求参数options.enterType=" + options.enterType);
              console.log("没值请求参fenxiangStr=" + JSON.stringify(fenxiangStr));
              if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
                console.log("没值不是分享进入的fenxiangStr=" + fenxiangStr);

              }else {
                console.log("没值是分享进入的fenxiangStr=" + fenxiangStr);
                if (fenxiangStr.enterType == 0) {//分享发展下级类型
                  var requestJson = {
                    parentId: fenxiangStr.parentId,
                    id: app.globalData.loginUserInfo.userInfo.id,
                    openId: app.globalData.loginUserInfo.userInfo.openId,
                    enterType: 0
                  }
                  console.log("没值发展下级requestJson=" + JSON.stringify(requestJson));
                  if (app.globalData.loginUserInfo.status != 100) {
                    that.memberLogin(requestJson,app);
                    //调用初始化方法
                  }
                }
              }
            }
          })
        },
        fail:function(res){
          console.log("wx.login失败="+JSON.stringify(res.data));
        },
        complete:function(res){
          wx.hideLoading();
          console.log("调用登录wx.login 完成" + JSON.stringify(res.data));
        }
      })
    }
  },

  //首页上下级关系逻辑
  shareFaZhanXiaJiIndex:function(app,options){
    var that=this;
    var res = wx.getStorageSync("loginUserInfo");
    var currentOpenId="";
    var currentId="";
    if (res) {
      app.globalData.loginUserInfo = res;
      console.log(app.globalData.baseUrl + "/member/login?" + app.globalData.loginUserInfo);
      console.log("有值二维码页面loginUserInfo=" + JSON.stringify(res));
      console.log("有值请求参数=options" + JSON.stringify(options));
      var fenxiangStr = options;
      console.log("有值请求参数options.parentId=" + options.parentId);
      console.log("没值请求参数options.enterType=" + options.enterType);
      console.log("没值请求参fenxiangStr=" + JSON.stringify(fenxiangStr));

      if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
        console.log("有值不是分享进入的fenxiangStr=" + fenxiangStr);
        var requestJson = {
          parentId: app.globalData.loginUserInfo.userInfo.parentId,
          id: app.globalData.loginUserInfo.userInfo.id,
          openId: app.globalData.loginUserInfo.userInfo.openId,
          enterType: 1
        }
        console.log("有值options没值发展下级requestJson=" + JSON.stringify(requestJson));
        if (app.globalData.loginUserInfo.status != 100) {
          that.memberLogin(requestJson,app);
          //调用初始化方法
        }
      }else {
        console.log("有值是分享进入的fenxiangStr=" + fenxiangStr);
        if (fenxiangStr.enterType == 0) {//分享发展下级类型
          var requestJson = {
            parentId: fenxiangStr.parentId,
            id: app.globalData.loginUserInfo.userInfo.id,
            openId: app.globalData.loginUserInfo.userInfo.openId,
            enterType: 0
          }
          console.log("有值有值发展下级requestJson=" + JSON.stringify(requestJson));
          if (app.globalData.loginUserInfo.status != 100) {
            that.memberLogin(requestJson,app);
            //调用初始化方法
          }
        }
      }
    } else {
      //app.cwxLoginGaiBan();
      //调用登录获取openId
      wx.login({
        success: function(res) {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res);
          var json = JSON.stringify({ code: res.code });
          wx.request({
            url: app.globalData.baseUrl + "/member/getWeChatMemberResult",
            data: json,
            header: app.globalData.headerPost,
            method: 'POST',
            success: function (e) {
              console.log("/member/getWeChatMemberResult 登录成功"+JSON.stringify(e.data));
              currentOpenId= e.data.data.openId;
              currentId = e.data.data.memberId;
            },
            fail: function (e) {
              console.log("/member/getWeChatMemberResult 调用失败="+e);
              wx.showLoading({
                title: '微信登录失败',
                mask: true
              });
            },
            complete:function(){
              wx.hideLoading();
              console.log("/member/getWeChatMemberResult 调用完成complete");
              console.log("没值值二维码页面loginUserInfo=" + JSON.stringify(res));
              console.log("没值请求参数=options" + JSON.stringify(options));
              var fenxiangStr = options;
              console.log("没值请求参数options.parentId=" + options.parentId);
              console.log("没值请求参数options.enterType=" + options.enterType);
              console.log("没值请求参fenxiangStr=" + JSON.stringify(fenxiangStr));
              if (fenxiangStr == 'undefined' || fenxiangStr == "" || fenxiangStr == null) {
                console.log("没值不是分享进入的fenxiangStr=" + fenxiangStr);

              }else {
                console.log("没值是分享进入的fenxiangStr=" + fenxiangStr);
                if (fenxiangStr.enterType == 0) {//分享发展下级类型
                  var requestJson = {
                    parentId: fenxiangStr.parentId,
                    id: app.globalData.loginUserInfo.userInfo.id,
                    openId: app.globalData.loginUserInfo.userInfo.openId,
                    enterType: 0
                  }
                  console.log("没值发展下级requestJson=" + JSON.stringify(requestJson));
                  if (app.globalData.loginUserInfo.status != 100) {
                    that.memberLogin(requestJson,app);
                    //调用初始化方法
                  }
                }else{
                  console.log("res没值，options为空进入的")
                  var requestJson = {
                    parentId: 0,
                    id: currentId,
                    openId: currentOpenId,
                    enterType: 1
                  }
                  console.log("没值发展下级requestJson=" + JSON.stringify(requestJson));
                  if (app.globalData.loginUserInfo.status != 100) {
                    that.memberLogin(requestJson,app);
                    //调用初始化方法
                  }
                }
              }
            }
          })
        },
        fail:function(res){
          console.log("wx.login失败="+JSON.stringify(res.data));
        },
        complete:function(res){
          wx.hideLoading();
          console.log("调用登录wx.login 完成" + JSON.stringify(res.data));
        }
      })
    }
  },
  //调用登录接口
  memberLogin:function(requestJson,app){
    var that=this;
    wx.request({
      url: app.globalData.baseUrl + "/member/login",
      data: JSON.stringify(requestJson),
      header:{ 'content-type': 'application/json'},
      method: 'POST',
      success: function (res) {
        console.log("调用发展下级接口成功=" + JSON.stringify(res.data));
        if (res.data.status == 200) {
          //调用初始化方法
          //that.minit(app);
          app.globalData.loginUserInfo.status=100;
          if(requestJson.enterType==0){
            app.globalData.loginUserInfo.userInfo.parentId = requestJson.parentId;
            app.globalData.loginUserInfo.userInfo.enterType = 0;
          }
          wx.setStorageSync("loginUserInfo", app.globalData.loginUserInfo);
          console.log("发展下级用户信息更新成功");
        } else {
          console.log("发展下级用户信息更新失败" + res.message);
        }

      }
    });
  },
  //获取用户id
  getMemberId:function(app){
    var res = wx.getStorageSync("loginUserInfo");
    if(!res){
      var memberId=0;
      wx.login({
        success: function(res) {
          var json = JSON.stringify({ code: res.code });
          wx.request({
            url: app.globalData.baseUrl + "/member/getWeChatMemberResult",
            data: json,
            header: app.globalData.headerPost,
            method: 'POST',
            success: function (e) {
              memberId= e.data.data.memberId;
            },
            fail: function (e) {
              console.log("/member/getWeChatMemberResult 调用失败="+e);
            },
            complete:function(){
              return memberId;
            }
          })
        }
      })
    }else {
      return res.userInfo.id;
    }
  },
  minit:function(app){
    var res = wx.getStorageSync("loginUserInfo");
    if(!res){
      wx.login({
        success: function(res) {
          var json = JSON.stringify({ code: res.code });
          wx.request({
            url: app.globalData.baseUrl + "/member/getWeChatMemberResult",
            data: json,
            header: app.globalData.headerPost,
            method: 'POST',
            success: function (e) {
              var initParamJson ={
                memberId:e.data.data.memberId,
                openId:e.data.data.openId
              };
              if(e.data.status==200) {
                wx.request({
                  url: app.globalData.baseUrl + "/member/init",
                  data: JSON.stringify(initParamJson),
                  header: app.globalData.headerPost,
                  method: 'POST',
                  success: function (e) {
                    console.log("初始化表成功");
                  }
                })
              }else{
                console.log("用户状态100没调用")
              }
            }
          })
        }
      })
    }else {
      var initParamJson ={
        memberId:res.userInfo.memberId,
        openId:res.userInfo.openId
      };
      wx.request({
        url: app.globalData.baseUrl + "/member/init",
        data: JSON.stringify(initParamJson),
        header: app.globalData.headerPost,
        method: 'POST',
        success: function (e) {
          console.log("初始化表成功");
        }
      })
    }
  }
  //地址接口
};